<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * This class is just for TripInformation CMS
 */
class Login extends PublicController {
	
	public function __construct() {
		parent::__construct();
		//Load helper
		$this->load->helper('common');
		$this->load->helper(array('form', 'url'));
		
		//Load library
		$this->load->library('session');
		
		//Load database
		$arr_load = array('user_model');
		$this->load->model($arr_load);
		$this->data['template_url'] = base_url('public').'/';
	}
	
	public function index(){
		$this->data['error'] = 0;
		
		//Redirect if session exist
		$session = $this->session->userdata('userid');
		if( $session )
			redirect( base_url('admin') );
		
		if( isset($_POST['login_submit']) ){
			$user = trim($_POST['username']);
			$pass = trim($_POST['password']);
			$where = array( 'username'=>$user,'password'=>md5($pass) );
			$rs_get = $this->user_model->get($where);
			
			if( !$rs_get )
				//login failed
				$this->data['error'] = 1;
			else{
				//Create session
				$session = array('userid'=>$rs_get[0]['id'],
								 'username'=>$rs_get[0]['username']);
				$this->session->set_userdata( $session );
				//login success
				redirect( base_url('admin') );
			}
		}
		$this->load->view('backend/login',$this->data);
	}
	
	public function logout(){
		$this->session->sess_destroy();
		redirect( base_url('login') );
	}
	
}
